package com.bwie.service.impl;

import com.bwie.pojo.Attachment;
import com.bwie.pojo.Shop;
import com.bwie.service.AttachmentService;
import com.bwie.service.ShopService;
import com.bwie.utils.R;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ContractMapper;
import com.bwie.pojo.Contract;
import com.bwie.service.ContractService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: Mikey
 * @Date:  2025/9/9 11:46
 * @Description: 
 **/

@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService{


    @Resource
    private AttachmentService attachmentService;

    @Resource
    private ContractMapper contractMapper;

    @Resource
    private ShopService shopService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveContract(Contract contract) {
        contract.setContractStatus(0);
        //先添加主表
        contractMapper.insert(contract);

        List<Attachment> attachments = new ArrayList<>();
        //添加成功返回主键ID  遍历添加附件对象
        for (String url : contract.getUrls()) {
            Attachment attachment = new Attachment();
            attachment.setContractId(contract.getId());
            attachment.setUrl(url);
            attachments.add(attachment);
        }
        attachmentService.saveBatch(attachments);
        return R.OK();
    }

    @Override
    public R getContractByShopId(String shopId, String categoryId) {
        Integer categoryIds = Integer.valueOf(categoryId);
        //去mapper层处理
        List<Contract> contracts=contractMapper.getContractByShopId(shopId,categoryIds);
        return R.OK(contracts);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R passStatus(Integer id) {
        //查询出该对象
        Contract contract = contractMapper.selectById(id);
        //赋值 通过
        contract.setContractStatus(1);
        //修改
        contractMapper.updateById(contract);
        Shop shop = shopService.getById(contract.getShopId());
        shop.setShopStatus(1);

        return R.OK();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R NopassStatus(Integer id) {
        //查询出该对象
        Contract contract = contractMapper.selectById(id);
        //赋值 拒绝
        contract.setContractStatus(2);
        //修改
        contractMapper.updateById(contract);
        Shop shop = shopService.getById(contract.getShopId());
        shop.setShopStatus(3);
        return R.OK();
    }
}
